import {VideoPlayMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity';

import worker, { MessageEvents, ThreadWorkerGlobalScope } from '@ohos.worker';
import { emitter } from '@kit.BasicServicesKit';
// import { VideoPlayerInfo } from './VideoPlayerMainMsgHandler';

class Data{
    type:string = "";
    function:string = "";
    viewTag:number = 0;
    value:string = "";
    event:number = 0;
    duration:number = 0;
}

export class VideoPlayerWorkerMsgHandler {
    private static workerPort:ThreadWorkerGlobalScope;

    static init(workerPort: ThreadWorkerGlobalScope) : void {
        VideoPlayerWorkerMsgHandler.workerPort = workerPort;
    }

    static getWorkerPort():ThreadWorkerGlobalScope{
        if(VideoPlayerWorkerMsgHandler.workerPort == null){
            VideoPlayerWorkerMsgHandler.workerPort = worker.workerPort
        }
        return VideoPlayerWorkerMsgHandler.workerPort;
    }

    static setURL(viewTag: number, url: string, isUrl: number) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity( 'setURL', viewTag);
        videoPlayMsgEntity.url = url;
        videoPlayMsgEntity.isUrl = isUrl;
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);

    }

    static setLooping(viewTag: number, isLoop: boolean) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('setLooping', viewTag);
        videoPlayMsgEntity.isLoop = isLoop;
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }

    static createVideoPlayer(viewTag: number,x: number, y: number, w: number, h: number) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('createVideoPlayer', viewTag);
        let viewInfo: ViewRect = new ViewRect(x, y, w, h);
        videoPlayMsgEntity.viewRect = viewInfo;
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }

    static removeVideoPlayer(viewTag: number) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('removeVideoPlayer', viewTag);
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }

    static setVideoPlayerRect(viewTag: number, x: number, y: number, w: number, h: number) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('setVideoPlayerRect', viewTag);
        let viewRect: ViewRect = new ViewRect(x, y, w, h);
        videoPlayMsgEntity.viewRect = viewRect;
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }

    static play(viewTag: number) : void {
        console.log("[VideoPlay]===============cpp call TS VideoPlayer play fcuntion========start=======");
        let callback = (eventData: emitter.EventData):void =>{
            let dataInfo: Data = eventData.data as Data;
            if(viewTag === dataInfo.viewTag){
                console.log("[VideoPlay]===============wait for source file prepared===============", dataInfo.viewTag);
                let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('play', dataInfo.viewTag);
                VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
                console.log("[VideoPlay]===============cpp call TS VideoPlayer play fcuntion========finish=======");
                emitter.off("onPreparedCallBack")
            }
        }
        emitter.on("onPreparedPlayEmitter", callback);

    }
    static pause(viewTag: number) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('pause', viewTag);
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }

    static stop(viewTag: number) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('stop', viewTag);
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }

    static setVisible(viewTag: number, visible: boolean) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('setVisible', viewTag);
        videoPlayMsgEntity.visible = visible
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }

    static requestFullscreen(viewTag: number, isFullScreen: boolean) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('requestFullscreen', viewTag);
        videoPlayMsgEntity.isFullScreen = isFullScreen;
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }

    static seekTo(viewTag: number, seekTo: number) : void {
        if(seekTo <= 0){
            return;
        }

        let callback = (eventData: emitter.EventData):void =>{
            let dataInfo: Data = eventData.data as Data;
            if(viewTag === dataInfo.viewTag && seekTo <= dataInfo.duration){
                let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('seekTo', viewTag);
                videoPlayMsgEntity.seekTo = seekTo;
                VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
            }
        }
        emitter.on("onPreparedSeekEmitter", callback);
    }

    static setKeepAspectRatioEnabled(viewTag: number, enable: boolean) : void {
        let videoPlayMsgEntity: VideoPlayMsgEntity = new VideoPlayMsgEntity('setKeepAspectRatioEnabled', viewTag);
        videoPlayMsgEntity.keepAspectRatioEnabled = enable;
        VideoPlayerWorkerMsgHandler.getWorkerPort().postMessage(videoPlayMsgEntity);
    }
}
